def FindOptMapping(mappings, scores, ntypeAvailable):

  maxCost <- ntypeAvailable
  maxTopologyID <- the total number of sub-topologies

  #Initialize Cost and Value
  For i in 0 to maxTopologyID:
    For j in 0 to maxCost:
      Cost[i,j] <- the number of machines required by the jth mapping in subnetwork i
      Value[i,j] <- the value of the jth mapping in subnetwork i

  For step in 0 to maxTopologyID - 1:
    For costNow in maxCost to 1:
      if step = 0:
        OptValue[0,costNow] <- Max(Value[0, j]; 0 < j <= maxCost, Cost[0,j] <= m)
        OptMap[step, costNow] <- (j, NULL)
      else:
        OptValue[step, costNow] <- 
          Max(Value[step,j] + OptValue[step-1, costNow - Cost[step,j]];
              0 < j <= maxCost, costNow - Cost[step, j] >0)
          OptMap[step, costNow] <- (j, costNow - Cost[step,j])

  #Traverse back to find all downscaled mappings for each sub-topology
  raw = maxCost
  For i in (maxTopologyID - 1) to 0:
    (mapID, raw) <- OptMap[i, raw]
    dsTopology[i] <- mappings[i, mapID]

  dsMap <- merge all sub-topologies in dsTopology

  return dsMap